using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Reflection;
using System.IO;
using System.Windows.Forms;

namespace ICSharpCode.TextEditor.Added
{
    public static class EditableHighlightService
    {
        private static XmlDocument _highlightDocument;

        public static IList<HightlightEntity> GetHighlightItems()
        {
            if (_highlightDocument == null)
            {
                LoadHighlightDocument();
            }

            XmlNodeList hightlightNodes = _highlightDocument.GetElementsByTagName("EditableItems");

            List<HightlightEntity> hightlightEntities = new List<HightlightEntity>();

            foreach (XmlNode node in hightlightNodes)
            {
                foreach (XmlNode editableItemNode in node.ChildNodes)
                {
                    HightlightEntity entity = new HightlightEntity();
                    // Used so it doesn't get marked dirty while just
                    // populating for the first time.
                    entity.Loading = true;

                    foreach (XmlNode childNode in editableItemNode.ChildNodes)
                    {
                        switch (childNode.Name)
                        {
                            case "Name" :
                                entity.Name = childNode.InnerText;
                                break;
                            case "DocumentLocation" :
                                entity.DocumentLocation = childNode.InnerText;
                                break;
                            case "TagName" :
                                entity.TagName = childNode.InnerText;
                                break;
                            case "AttributeName" :
                                entity.AttributeName = childNode.InnerText;
                                break;
                        }
                    }

                    string foreColorName = string.Empty;
                    string backColorName = string.Empty;
                    string executingPath = Path.GetDirectoryName(Application.ExecutablePath);

                    GetColors(executingPath + entity.DocumentLocation, entity.TagName, entity.AttributeName, ref foreColorName, ref backColorName);

                    if (foreColorName.Length > 0)
                    {
                        entity.ForeColorName = foreColorName;
                    }

                    if (backColorName.Length > 0)
                    {
                        entity.BackColorName = backColorName;
                    }

                    entity.Loading = false;

                    hightlightEntities.Add(entity);
                }
            }
         
            return hightlightEntities;
        }

        private static void LoadHighlightDocument()
        {
            _highlightDocument = new XmlDocument();
            Assembly assembly = typeof(EditableHighlightService).Assembly;

            // Make sure the current dir is here.
            //Environment.CurrentDirectory = Path.GetDirectoryName(assembly.Location);

            _highlightDocument.Load(assembly.GetManifestResourceStream("ICSharpCode.TextEditor.Src.Added.EditableHighlightItems.xml"));

        }

        public static void GetColors(string document, string tagName, string attributeName, ref string foregroundColor, ref string backgroundColor)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(document);

            XmlNodeList hightlightNodes = doc.GetElementsByTagName(tagName);
            foreach (XmlNode node in hightlightNodes)
            {
                if (node.Attributes["name"].Value == attributeName)
                {
                    foregroundColor = node.Attributes["color"].Value;
                    if (node.Attributes["bgcolor"] != null)
                    {
                        backgroundColor = node.Attributes["bgcolor"].Value;
                    }
                }
            }
        }

        public static void SetColors(string document, string tagName, string attributeName, string foregroundColor, string backgroundColor)
        {
            XmlDocument doc = new XmlDocument();
          
            doc.Load(document);

            XmlNodeList hightlightNodes = doc.GetElementsByTagName(tagName);
            foreach (XmlNode node in hightlightNodes)
            {
                if (node.Attributes["name"].Value == attributeName)
                {
                    node.Attributes["color"].Value = foregroundColor;
                    if (node.Attributes["bgcolor"] != null)
                    {
                        node.Attributes["bgcolor"].Value = backgroundColor;
                    }
                }
            }

            doc.Save(document);
        }

        public static void SaveNewColors(IList<HightlightEntity> entities)
        {
            foreach (HightlightEntity entity in entities)
            {
                if (entity.Dirty)
                {
                    SetColors(Path.GetDirectoryName(Application.ExecutablePath) + entity.DocumentLocation, entity.TagName, entity.AttributeName, entity.ForeColorName, entity.BackColorName);
                }
            }
        }
    }
}
